import global from "../global";
import config from "../config";

const { ccclass, property } = cc._decorator;

@ccclass
export default class playerInfoNode extends cc.Component {

    @property({
        type: cc.Sprite,
        displayName: "头像"
    })
    imageNode: cc.Sprite = null;

    @property({
        type: cc.Label,
        displayName: "昵称"
    })
    nameNode: cc.Label = null;

    private Info: PlayerInfos = null;
    public loadingInfo(){
        // 加载头像

        // 如果有信息就加载玩家头像
        if(!!global.player && !!global.player.avatar){
        // if (!!this.Info && !!this.Info.avatar) {
            let url = global.player.avatar;
            cc.assetManager.loadRemote(url, { ext: '.jpg' }, (err, texture) => {
                this.imageNode.spriteFrame = new cc.SpriteFrame(texture); // 传入图片
            });
            //console.log("123");
            // cc.loader.load({
            //     url: url,
            //     type: 'jpg'
            // },
            //     (err, texture) => {
            //         if (err)
            //             return;
            //         this.imageNode.spriteFrame = new cc.SpriteFrame(texture);
            //     })
        }
        //this.nameNode.string = !!global.player && !!global.player.nickname ? global.player.nickname : "暂无信息";
        this.nameNode.string = !!global.player ? global.player.nickname : "未登录";
    }

    public getInfo = (PlayerInfo: PlayerInfos): void => {
        this.Info = PlayerInfo;
        this.loadingInfo();
        //console.log("get:"+ this.Info.nickname +")");
    };

    onLoad() {
    }

    start() {
        this.loadingInfo();
    }
}
